# coding=utf-8
# Author: hang.zhang
# Date: 2024-06-28 17:19:45
# LastEditors: hang.zhang
# LastEditTime: 2024-06-28 22:21:36

import requests

from loguru import logger

COOKIE = "reusername=13544276652; repassword=0oOo%29_%28_yxsrC2sbGztLe3trM%3D; jys2frontsid=959b06fd-24ea-4843-862f-d9b81e08a13b"

QUERY_JSON = {
    "pageCurrent": 1,
    "pageSize": 10,
    "yxsd": "",
    "rmcs": "",
    "bxlb": "",
    "xxlb": "",
    "lqpc": "1",
    "sf985": 0,
    "sf211": 0,
    "sfsyl": 0,
    "sfqt": 0,
    "tjlb": "0",
    # 选择物理和化学
    "xzkm": "3,4",
    "zydms": "",
    # 是否中外合作
    "sfbhzwhz": "0",
    # 是否国家专项计划
    "sfbhgjzx": "1",
    # 是否地方专项计划
    "sfbhdfzx": "0",
    "sfbhssykb": "1",
    "sfbhmzb": "1",
    "sfbhcygj": "1",
    "yxmc": ""
}

QUERY_JSON = {
    "maxzdtwf": "635",
    "minzdtwf": "600",
    
    "pageCurrent": 1,
    "pageSize": 10,
    "yxsd": "",
    "rmcs": "",
    "bxlb": "",
    "xxlb": "",
    "lqpc": "1",
    "sf985": 0,
    "sf211": 0,
    "sfsyl": 0,
    "sfqt": 0,
    "tjlb": "0",
    "xzkm": "3,4",
    "zydms": "",
    # 是否中外合作
    "sfbhzwhz": "0",
    # 是否国家专项计划
    "sfbhgjzx": "1",
    # 是否地方专项计划
    "sfbhdfzx": "0",
    "sfbhssykb": "1",
    "sfbhmzb": "1",
    "sfbhcygj": "1",
    "yxmc": ""
}

QUERY_JSON_ALL = {
    "pageCurrent": 1,
    "pageSize": 5,
    "yxmc": "",
    "yxsd": "",
    "rmcs": "",
    "bxlb": "",
    "xxlb": "",
    "lqpc": "1",
    "sf985": 0,
    "sf211": 0,
    "sfsyl": 0,
    "sfqt": 0,
    "xzkm": "3,4",
    
    # "minzdtwf": "600",
    # "maxzdtwf": "631",

    "minzdtwf": "600",
    "maxzdtwf": "700",

    "xzyx": "0",
    "sfbhzwhz": "0",
    "sfbhgjzx": "1",
    "sfbhdfzx": "0",
    "sfbhssykb": "1",
    "sfbhmzb": "1",
    "sfbhcygj": "1"
}

def get_detail(query_json):
    r = requests.post(
        "https://www.dxlql.com/schoolMajor/tjSchoolMajorPage",
        headers={
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": COOKIE,
        },
        json=query_json
    )
    return r

def get_detail_type2(query_json):
    r = requests.post(
        # "https://www.dxlql.com/schoolMajor/tjSchoolMajorPage",
        "https://www.dxlql.com/schoolMajor/selectSchoolMajorPage",
        headers={
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": COOKIE,
        },
        json=query_json
    )
    return r

def get_all_detail():
    result = []
    for tjlb in ["0", "1", "2",]:
    # for tjlb in ["0", "1",]:
        cur_query_json = QUERY_JSON.copy()
        cur_query_json["tjlb"] = tjlb
        total_page = float("inf")
        cur_page = 2
        
        while cur_page <= total_page + 2:
            cur_query_json["pageCurrent"] = cur_page
            r = get_detail(cur_query_json)
            _content = r.json()
            if tjlb == "0":
                _content["myremark"] = "冲"
            elif tjlb == "1":
                _content["myremark"] = "稳"
            elif tjlb == "2":
                _content["myremark"] = "保"
            
            _content["page_idx"] = cur_page
            result.append(_content)
            
            total_page = r.json()["data"]["data"]["totalPage"]
            logger.info(f"req page: [{cur_page} / {total_page}]")
            cur_page += 1
            # break
    return result
    


def get_all_detail_type2():
    result = []
    cur_query_json = QUERY_JSON_ALL.copy()

    total_page = float("inf")
    cur_page = 2
    
    while cur_page <= total_page + 2:
        cur_query_json["pageCurrent"] = cur_page
        r = get_detail_type2(cur_query_json)
        _content = r.json()

        
        _content["page_idx"] = cur_page
        result.append(_content)
        
        total_page = r.json()["data"]["totalPage"]
        logger.info(f"req page: [{cur_page} / {total_page}]")
        cur_page += 1
        # break
    return result

def get_zyz_detail(zyz_id):
    r = requests.get(
        f"https://www.dxlql.com/schoolMajor/searchZyzMajorList//{zyz_id}",
        headers={
            "Content-Type": "application/json;charset=UTF-8",
            "Cookie": COOKIE,
        },
    )
    return r.json()


if __name__ == "__main__":
    print(get_detail())

